package org.alala.user.common.dao.mysql;

import java.util.List;

import org.alala.datamodels.common.query.Query;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.beans.factory.annotation.Autowired;

public abstract class MysqlDaoBase extends SqlSessionDaoSupport {
	
	//mapper文件的命名空间
	public abstract String namespace();
	
	@Autowired
	public void setServiceSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
		super.setSqlSessionFactory(sqlSessionFactory);
	}
	
	protected <T> List<T> selectList(String name, Query query) {
		return getSqlSession().selectList(buildFullName(name), query);
	}
	
	protected <T> T selectOne(String name, Query query) {
		return getSqlSession().selectOne(buildFullName(name), query);
	}
	
	protected <T> int insert(String name, T obj) {
		return getSqlSession().insert(buildFullName(name), obj);
	}
	
	private String buildFullName(String name) {
		return String.format("%s.%s", namespace(), name);
	}
}
